const dgram =require('dgram');

const server = dgram.createSocket('udp4');

// 广播：
server.on('listening',()=>{
    var address=server.address();
    server.setBroadcast(true);
    server.setMulticastTTL(128);
    setInterval(()=>{
        console.log('发送消息')
        server.send('我已经上线了！',5000,'127.0.0.1')
        server.send('我已经上线了！',5002,'127.0.0.1')
    },3000)
    console.log('listening on:'+address.address+':'+address.port)
})
// 单播发送数据
server.on('message',(message,remote)=>{
    console.log('收到了你的消息: ', message.toString());
    const address = remote.address;
    console.log('remote.address: ', remote.address);
    const port = remote.port;
    console.log('remote.port: ', remote.port);
    // server.send('我已经收到你们发送的消息了！',port,address)
})
server.on('error',()=>{
    console.log('服务其出错了');
})
//绑定8888端口
server.bind(4000)